package net.code.btalk.ui;

import java.io.IOException;

import com.hoodemia.core.WebServiceHandler;
import com.hoodemia.ui.StandardButton;
import com.hoodemia.ui.StandardPasswordField;
import com.hoodemia.ui.StandardTextField;

import net.code.btalk.BTalk;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.DialogClosedListener;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import org.json.me.JSONArray;
import org.json.me.JSONException;

public class SignUpScreen extends FullScreen{
	public static BTalk btalk;

	public SignUpScreen() {
		super(FullScreen.NO_VERTICAL_SCROLL & FullScreen.NO_VERTICAL_SCROLLBAR);

		this.addKeyListener(new GenericKeyListener());

		Background background = BackgroundFactory.createBitmapBackground(Bitmap
				.getBitmapResource(Constants.IMAGE_BACKGROUND_LOGIN));
		this.setBackground(background);

		LabelField titleBar = new LabelField("Register to Hoodemia",
				LabelField.USE_ALL_WIDTH | LabelField.HCENTER) {

			protected void paint(Graphics graphics) {
				graphics.setColor(Color.WHITE);
				super.paint(graphics);
			}

		};
		titleBar.setBackground(Constants.TITLEBAR_BACKGROUND_BLACK_GRADIENT);

		this.add(titleBar);
		this.add(new LabelField());
		this.add(new LabelField());
		
		// create register form
		VerticalFieldManager formSection = new VerticalFieldManager(
				Field.FIELD_HCENTER);

		// configure username section
		HorizontalFieldManager usernameSection = new HorizontalFieldManager(
				Field.FIELD_HCENTER);
		usernameSection.setPadding(Constants.FORM_SECTION_H_PADDING);

		Bitmap usernameIcon = Bitmap
				.getBitmapResource(Constants.IMAGE_USERNAME_ICON);
		BitmapField usernameIconField = new BitmapField(usernameIcon);
		usernameIconField.setPadding(Constants.ICON_FIELD_PADDING);

		// username field
		final StandardTextField usernameField = new StandardTextField("username", 100, StandardTextField.FIELD_HCENTER & StandardTextField.NO_NEWLINE);

		usernameSection.add(usernameIconField);
		usernameSection.add(usernameField);

		formSection.add(usernameSection);
		formSection.add(new LabelField());

		// configure email address section
		HorizontalFieldManager emailAddressSection = new HorizontalFieldManager(
				Field.FIELD_HCENTER);
		emailAddressSection.setPadding(Constants.FORM_SECTION_H_PADDING);

		Bitmap emailAddressIcon = Bitmap
				.getBitmapResource(Constants.IMAGE_EMAIL_ICON);
		BitmapField emailAddressIconField = new BitmapField(emailAddressIcon);
		emailAddressIconField.setPadding(Constants.ICON_FIELD_PADDING);

		// email address field
		final StandardTextField emailAddressField = new StandardTextField("email address", 100, StandardTextField.FIELD_HCENTER & StandardTextField.NO_NEWLINE);

		emailAddressSection.add(emailAddressIconField);
		emailAddressSection.add(emailAddressField);

		formSection.add(emailAddressSection);
		formSection.add(new LabelField());

		// configure password section
		HorizontalFieldManager passwordSection = new HorizontalFieldManager(
				Field.FIELD_HCENTER);
		passwordSection.setPadding(Constants.FORM_SECTION_H_PADDING);

		Bitmap passwordIcon = Bitmap
				.getBitmapResource(Constants.IMAGE_PASSWORD_ICON);
		BitmapField passwordIconField = new BitmapField(passwordIcon);
		passwordIconField.setPadding(Constants.ICON_FIELD_PADDING);

		// password field
		final StandardPasswordField passwordField = new StandardPasswordField("password", 100, StandardTextField.FIELD_HCENTER & StandardTextField.NO_NEWLINE);

		passwordSection.add(passwordIconField);
		passwordSection.add(passwordField);

		formSection.add(passwordSection);
		formSection.add(new LabelField());

		// confirm password section
		HorizontalFieldManager confirmPasswordSection = new HorizontalFieldManager(
				Field.FIELD_HCENTER);
		confirmPasswordSection.setPadding(Constants.FORM_SECTION_H_PADDING);

		Bitmap confirmPasswordIcon = Bitmap
				.getBitmapResource(Constants.IMAGE_PASSWORD_CONFIRM_ICON);
		BitmapField confirmPasswordIconField = new BitmapField(
				confirmPasswordIcon);
		confirmPasswordIconField.setPadding(Constants.ICON_FIELD_PADDING);

		// confirm password field
		final StandardPasswordField confirmPasswordField = new StandardPasswordField("confirm password", 100, StandardTextField.FIELD_HCENTER & StandardTextField.NO_NEWLINE);

		confirmPasswordSection.add(confirmPasswordIconField);
		confirmPasswordSection.add(confirmPasswordField);

		formSection.add(confirmPasswordSection);
		formSection.add(new LabelField());
		
		FieldChangeListener signupButtonListener = new FieldChangeListener() {

			public void fieldChanged(Field field, int context) {
				String usernameStr = usernameField.getText();
				String emailAddressStr = emailAddressField.getText();
				String passwordStr = passwordField.getText();
				String confirmPasswordStr = confirmPasswordField.getText();
				if (usernameStr.equals(null) || emailAddressStr.equals(null)
						|| passwordStr.equals(null)
						|| confirmPasswordStr.equals(null)
						|| usernameStr.equals("") || emailAddressStr.equals("")
						|| passwordStr.equals("")
						|| confirmPasswordStr.equals("")) {
					Dialog.alert("You can not leave any field blank.");
				} else if (passwordStr.equals(confirmPasswordStr)) {
					btalk.registerJxa(usernameStr, passwordStr, emailAddressStr);
					
					Dialog dialog = new Dialog(
							Dialog.D_OK,
							"Congratulations, you have successfully signed up to hoodemia!",
							0, Bitmap.getBitmapResource("img/check_green.png"), Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
					int i = dialog.doModal();
					if(i == 0) {
						UiApplication.getUiApplication().popScreen(SignUpScreen.this);
					}
					
//	   				Dialog.alert("Congratulations, you have successfully signed up to hoodemia!");
//					Dialog.alert("Username: " + usernameField.getText()
//							+ "Email: " + emailAddressField.getText()
//							+ "Password: " + passwordField.getText());
//					SignUpScreen.this.signup(usernameStr, passwordStr, confirmPasswordStr, emailAddressStr);
				} else {
					Dialog.alert("Password does not match");
				}
			}

		};

		FieldChangeListener cancelButtonListener = new FieldChangeListener() {

			public void fieldChanged(Field field, int context) {
				UiApplication.getUiApplication().popScreen(
						UiApplication.getUiApplication().getActiveScreen());
			}

		};
		
		// configure button section
		HorizontalFieldManager buttonSection = new HorizontalFieldManager(Field.FIELD_HCENTER);

		StandardButton signupButton = new StandardButton("Sign Up", StandardButton.FIELD_HCENTER, StandardButton.PADDING_SMALL);
		signupButton.setChangeListener(signupButtonListener);		
		signupButton.setMargin(0, 30, 0, 0);

		buttonSection.add(signupButton);
		
		StandardButton cancelButton = new StandardButton("Cancel", StandardButton.FIELD_HCENTER, StandardButton.PADDING_SMALL);
		cancelButton.setChangeListener(cancelButtonListener);
		cancelButton.setMargin(0, 0, 0, 30);
		
		buttonSection.add(cancelButton);
		
		formSection.add(buttonSection);
		
		this.add(formSection);
	}
	
	/*private void signup(String username, String password, String confirmPassword, String email) {
		String error = "";
	    try {
			JSONArray serverResponse = WebServiceHandler.getInstance().SignUp(username, password, confirmPassword, email);
   			String status = serverResponse.getString(0);
   			if(status.equalsIgnoreCase("false") || status.equalsIgnoreCase("null")) {
   				//Incorrect username
   				Dialog.alert("Sorry, your request could not be completed");
   			}
   			else {
				Dialog dialog = new Dialog(
						Dialog.D_OK,
						"Congratulations, you have successfully signed up to hoodemia!",
						0, Bitmap.getBitmapResource("img/check_green.png"), Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
				int i = dialog.doModal();
				if(i == 0) {
					UiApplication.getUiApplication().popScreen(SignUpScreen.this);
				}
//   				Dialog.alert("Congratulations, you have successfully signed up to hoodemia!");
   			}
	    }
	    catch (JSONException e) {
   			// TODO Auto-generated catch block
   			e.printStackTrace();
   		}
   		catch (NullPointerException e) {
 	   		add(new RichTextField("null"));
 	   		e.printStackTrace();
 	   	}
   		catch (NumberFormatException e) {
 	   		add(new RichTextField(""));
 	   		e.printStackTrace();
 	   	}
   		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Dialog.alert("Connection error.\nPlease check your network option.");
	    }
	}*/
}
